package com.cfets.annualAssessment;

import com.cfets.annualAssessment.filter.MyFilterSecurityInterceptor;
import com.cfets.annualAssessment.securityConfig.MyAccessDecisionManager;
import com.cfets.annualAssessment.securityConfig.MyInvocationSecurityMetadataSource;
import com.cfets.annualAssessment.util.RedisRunner;
import com.cfets.annualAssessment.weixinService.MyWxMpServiceImpl;
import com.cfets.annualAssessment.weixinService.bean.WxMpInRedisConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator;
import org.springframework.security.web.access.WebInvocationPrivilegeEvaluator;


@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
//extends SpringBootServletInitializer
public class AppApplication extends SpringBootServletInitializer{

	public static void main(String[] args) {
		SpringApplication.run(AppApplication.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(AppApplication.class);
	}
	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {
		return (container -> {
			ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
			ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
			container.addErrorPages(error404Page, error500Page);
		});
	}
	@Bean
	public PasswordEncoder passwordEncoder() {
		return new StandardPasswordEncoder();
	}

	@Bean(name = "myInvocationSecurityMetadataSource")
	public MyInvocationSecurityMetadataSource myInvocationSecurityMetadataSource(){
		return new MyInvocationSecurityMetadataSource();
	}

	@Bean(name = "myAccessDecisionManager")
	public MyAccessDecisionManager myAccessDecisionManager(){
		return new MyAccessDecisionManager();
	}

	@Bean
	public MyFilterSecurityInterceptor myFilterSecurityInterceptor(MyInvocationSecurityMetadataSource myInvocationSecurityMetadataSource,
																   MyAccessDecisionManager myAccessDecisionManager
																   ){
		MyFilterSecurityInterceptor myFilterSecurityInterceptor = new MyFilterSecurityInterceptor();
		myFilterSecurityInterceptor.setAccessDecisionManager(myAccessDecisionManager);
		myFilterSecurityInterceptor.setSecurityMetadataSource(myInvocationSecurityMetadataSource);
		return myFilterSecurityInterceptor;
	}

	@Bean
	public WebInvocationPrivilegeEvaluator webInvocationPrivilegeEvaluator(MyFilterSecurityInterceptor myFilterSecurityInterceptor) {
		return new DefaultWebInvocationPrivilegeEvaluator(myFilterSecurityInterceptor);
	}

//	@Bean
//	public DefaultWebInvocationPrivilegeEvaluator defaultWebInvocationPrivilegeEvaluator(MyFilterSecurityInterceptor myFilterSecurityInterceptor){
//		DefaultWebInvocationPrivilegeEvaluator defaultWebInvocationPrivilegeEvaluator =
//				new DefaultWebInvocationPrivilegeEvaluator(myFilterSecurityInterceptor);
//		return defaultWebInvocationPrivilegeEvaluator;
//	}
	@Bean
	public RedisRunner redisRunner(){
		return new RedisRunner("127.0.0.1:6379");
	}
	@Bean(name = "wxMpConfigStorage")
	public WxMpInRedisConfigStorage wxMpInRedisConfigStorage(RedisRunner redisRunner){
		WxMpInRedisConfigStorage wxMpInRedisConfigStorage = new WxMpInRedisConfigStorage();
		wxMpInRedisConfigStorage.setRedisRunner(redisRunner);
		return wxMpInRedisConfigStorage;
	}
	@Bean(name = "mpService")
	public MyWxMpServiceImpl myWxMpService (WxMpInRedisConfigStorage wxMpInRedisConfigStorage){
		MyWxMpServiceImpl myWxMpService = new MyWxMpServiceImpl();
		myWxMpService.setWxMpConfigStorage(wxMpInRedisConfigStorage);
		return myWxMpService;
	}

	@Bean
	public WxMpMessageRouter wxMpMessageRouter(MyWxMpServiceImpl myWxMpService){
		return new WxMpMessageRouter(myWxMpService);
	}

}

